package oop.controller.change.question;

import oop.controller.change.ChangeDelegate;
import oop.data.BaseQuestion;
import oop.data.Text;
import oop.db.dao.QuestionDAO;
import oop.db.dao.TextDAO;

public class QuestionContentChange extends ChangeDelegate {
	
	private long questionId;
	private long oldContentId;
	private long newContentId;

	/**
	 * For GSON only
	 */
	public QuestionContentChange() {
	}
	
	public QuestionContentChange(long questionId, long newContentId) {
		this();
		this.questionId = questionId;
		this.newContentId = newContentId;
	}

	@Override
	public void perform() throws Exception {
		BaseQuestion question = QuestionDAO.fetchByIdBase(questionId);
		Text newContent = TextDAO.fetch(newContentId);
		oldContentId = question.getContent().getId();
		question.setContent(newContent);
		QuestionDAO.persist(question);
	}

	@Override
	public boolean isReversible() {
		return true;
	}
	
	@Override
	public ChangeDelegate createReverse() {
		return new QuestionContentChange(questionId, oldContentId);
	}

	@Override
	public String getSummary() throws Exception {
		return "đã sửa đổi nội dung câu hỏi";
	}

}
